/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PANDA_TOOLING_TEST_JS_ENUMERATE_FRAMES_TEST_H
#define PANDA_TOOLING_TEST_JS_ENUMERATE_FRAMES_TEST_H

#include "test_util.h"

namespace ark::tooling::test {
class JsEnumerateFrameTest : public ApiTest {
public:
    JsEnumerateFrameTest()
    {
        vmDeath = [this]() {
            ASSERT_EQ(countFrames_, 9U);
            return true;
        };

        methodExit = [this](PtThread thread, Method *method, bool, VRegValue) {
            if (method->GetFullName() == "_GLOBAL::func_method3_3") {
                ASSERT_EQ(countFrames_, 0U);
                debugInterface->EnumerateFrames(thread, callback_);
                ASSERT_EQ(countFrames_, 4U);
            } else if (method->GetFullName() == "_GLOBAL::func_method2_2") {
                ASSERT_EQ(countFrames_, 4U);
                debugInterface->EnumerateFrames(thread, callback_);
                ASSERT_EQ(countFrames_, 7U);
            } else if (method->GetFullName() == "_GLOBAL::func_method1_1") {
                ASSERT_EQ(countFrames_, 7U);
                debugInterface->EnumerateFrames(thread, callback_);
                ASSERT_EQ(countFrames_, 9U);
            }
            return true;
        };
    }

    std::pair<const char *, const char *> GetEntryPoint() override
    {
        return {pandaFile_.c_str(), entryPoint_.c_str()};
    }

private:
    std::string pandaFile_ = "js/GetFrame.abc";
    std::string entryPoint_ = "_GLOBAL::func_main_0";
    size_t countFrames_ = 0;

    std::function<bool(const PtFrame &debugFrame)> callback_ {[this](const PtFrame &debugFrame) {
        this->countFrames_++;
        return debugFrame.GetArgumentNum() != 1;
    }};
};

inline std::unique_ptr<ApiTest> JsEnumerateFramesTest()
{
    return std::make_unique<JsEnumerateFrameTest>();
}
}  // namespace ark::tooling::test

#endif  // PANDA_TOOLING_TEST_JS_ENUMERATE_FRAMES_TEST_H
